import { Loading } from 'element-ui';

let loadingCount = 0;
let loading;
let time = 5

const startLoading = (msg) => {
  time = msg;
  const timer = setInterval(() => {

    if (time === 0) {

      clearInterval(timer);

      loading.close();
      time =5;

    } else {
      loading.text =  time + "s后退出";
      --time;

    }

  }, 1000)


  loading = Loading.service({
    lock: true,
    text:  time + "s后退出",
  background: 'rgba(0, 0, 0, 0.7)'



});
};

const endLoading = () => {
  loading.close();
};

export const showLoading = (msg) => {
  if (loadingCount === 0) {
    startLoading(msg);
  }
  loadingCount += 1;
};

export const hideLoading = () => {
  if (loadingCount <= 0) {
    return;
  }
  loadingCount -= 1;
  if (loadingCount === 0) {
    endLoading();
  }
};
